﻿using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using TianYu.CMS.Domain.DomainModel;
using TianYu.CMS.Domain.ViewModel.Request;
using TianYu.CMS.Service;
using TianYu.Core.Common;

namespace TianYu.CMS.WebMvc.Areas.Admin.Controllers
{
    public class SysConfigController : BaseController
    {
        private ISysConfigService _sysConfigService;

        public SysConfigController(ISysConfigService sysConfigService)
        {
            this._sysConfigService = sysConfigService;
        }




        public async Task<IActionResult> Index()
        {
            var entity = new SysConfigViewMode();
            var en = await _sysConfigService.GetAsync(o => o.ConfigCode == "baseConfig");
            if (en != null)
            {
                if (!en.ConfigContent.IsNullOrWhiteSpace())
                {
                    entity = JsonHelper.DeserializeObject<SysConfigViewMode>(en.ConfigContent);
                }
            }
            return View(entity);
        }

        public async Task<AjaxResult<bool>> Save(SysConfigViewMode requestModel)
        {
            string configContent = JsonHelper.SerializeObject(requestModel);

            var result = await _sysConfigService.UpdateAsync(o => new SysConfig() { ConfigContent = configContent }, x => x.ConfigCode == "baseConfig");

            return result ? AjaxResult<bool>.Success() : AjaxResult<bool>.Fail("保存失败");
        }

    }
}